package cn.com.nes.site.service.system.easyRepair.impl;

import cn.com.nes.common.em.platform.EasyRepairMethodEnum;
import cn.com.nes.common.util.HttpUtils;
import cn.com.nes.site.entity.bo.user.PushEasyRepairUserInfoBO;
import cn.com.nes.site.service.system.easyRepair.EasyRepairService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 易修实现
 *
 * @Author: ShenWei
 * @Date: 2024/05/22/13:59
 */
@Service
@Slf4j
public class EasyRepairServiceImpl implements EasyRepairService {

    // 创建易修用户
    @Override
    public String pushEasyRepairUser(PushEasyRepairUserInfoBO easyRepairUserInfoBo) {
        JSONObject paramJson = JSON.parseObject(JSON.toJSONString(easyRepairUserInfoBo));
        log.info("paramJson: {}", paramJson);
        // 获取接口url
        String url = EasyRepairMethodEnum.AGENT_CREATE_USER.url();
        log.info("pushEasyRepairUserUrl: {}", url);
        // 调接口
        String result = HttpUtils.sendJSONPost(url, paramJson.toJSONString());
        log.info("easyRepairUserResult:{}", result);
        // 解析返回值
        JSONObject resultJson = JSON.parseObject(result);
        // success:code = 0 / error:code = 1
        if (ObjectUtil.isNotNull(resultJson) && 0 == resultJson.getInteger("code") && ObjectUtil.isNotNull(resultJson.getJSONObject("data"))) {
            JSONObject easyRepairUser = resultJson.getJSONObject("data");
            log.info("创建易修用户成功!");
            return String.valueOf(easyRepairUser.get("id"));
        } else {
            log.info("创建易修用户失败!");
            return StrUtil.EMPTY;
        }
    }

    // 删除易修用户
    @Override
    public void pushDeleteEasyRepairUserInfo(Long easyRepairUserId) throws Exception {
        JSONObject paramJson = new JSONObject();
        paramJson.put("easyRepairUserId", easyRepairUserId);
        log.info("paramJson: {}", paramJson);
        // 获取接口url
        String url = EasyRepairMethodEnum.AGENT_DELETE_USER.url();
        log.info("pushDeleteEasyRepairUserInfoUrl: {}", url);
        // 调接口
        String result = HttpUtils.sendJSONPost(url, paramJson.toJSONString());
        log.info("pushDeleteEasyRepairUserInfoResult:{}", result);
        // 解析返回值
        JSONObject resultJson = JSON.parseObject(result);
        // success:code = 0 / error:code = 1
        if (ObjectUtil.isNotNull(resultJson) && 0 == resultJson.getInteger("code")) {
            log.info("删除易修用户成功!");
        } else {
            log.info("删除易修用户失败!");
            throw new Exception();
        }
    }
}
